package com.ie.service.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ie.common.http.ResponseResult;
import com.ie.entity.GoodsTypeEntity;
import com.ie.mapper.admin.GoodsTypeMapper;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class GoodsTypeServiceImpl extends ServiceImpl<GoodsTypeMapper, GoodsTypeEntity> implements GoodsTypeService {
    @Override
    public ResponseResult<Map<String, Object>> addGoodsType(GoodsTypeEntity goodsTypeEntity) {
        boolean res = save(goodsTypeEntity);
        if(res)
        {
            return ResponseResult.getMessageResult(null, "A001");
        }
        return ResponseResult.getMessageResult(null, "A002");
    }

    @Override
    public ResponseResult<Map<String, Object>> deleteGoodsType(GoodsTypeEntity goodsTypeEntity) {
        long res = 0;
        if(res > 0)
        {
            return ResponseResult.getMessageResult(null, "A001");
        }
        if(removeById(goodsTypeEntity.getId()))
        {
            return ResponseResult.getMessageResult(null, "A002");
        }
        return ResponseResult.getMessageResult(null, "A003");
    }

    @Override
    public ResponseResult<Map<String, Object>> updateGoodsType(GoodsTypeEntity goodsTypeEntity) {
        if(updateById(goodsTypeEntity))
        {
            return ResponseResult.getMessageResult(null, "A001");
        }
        return ResponseResult.getMessageResult(null, "A002");
    }

    @Override
    public ResponseResult<Map<String, Object>> getGoodsType(GoodsTypeEntity goodsTypeEntity) {
        Integer currentPage = goodsTypeEntity.getCurrentPage();
        if(currentPage == null || currentPage < 0)
        {
            currentPage = 1;
        }

        IPage<GoodsTypeEntity> ipage = new Page<>(currentPage, 10);
        IPage<GoodsTypeEntity> page = this.page(ipage);
        Map<String, Object> npapresult = new HashMap<>();
        npapresult.put("allGoodsTypes", page.getRecords());
        npapresult.put("total", page.getPages());
        return ResponseResult.getSuccessResult(npapresult);
    }
}
